package cn.fl.project.service.impl;

import cmm.comm.facade.framework.api.ISysCdMapApiService;
import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.condition.SysCdMapDQueryCondition;
import cmm.comm.facade.framework.dto.SysCdMapDDTO;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.audit.service.IAudStandingBookService;
import cn.fl.audit.service.IAuditApiService;
import cn.fl.capital.condition.CapPayInfoQueryCondition;
import cn.fl.capital.constant.CapitalConstant;
import cn.fl.capital.service.ICapPayInfoService;
import cn.fl.capital.vo.CapPayInfoVO;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.customer.vo.CstBscInfoVO;
import cn.fl.expatriate.condition.AdvBizRecQC;
import cn.fl.expatriate.service.IAdvBizRecService;
import cn.fl.expatriate.vo.AdvBizRecVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.api.IProjectQueryApiService;
import cn.fl.project.condition.PrjCstAccInfoQueryCondition;
import cn.fl.project.condition.PrjCstBscInfoQueryCondition;
import cn.fl.project.condition.PrjQuatPrpsFeeQueryCondition;
import cn.fl.project.condition.PrjQuatPrpsQueryCondition;
import cn.fl.project.constant.PrjBscInfoConstant;
import cn.fl.project.constant.PrjCstAccInfoConstant;
import cn.fl.project.constant.PrjCstBscInfoConstant;
import cn.fl.project.constant.PrjQuatPrpsFeeConstant;
import cn.fl.project.dto.PrjBscInfoDTO;
import cn.fl.project.dto.PrjCstAccInfoDTO;
import cn.fl.project.dto.ProjectFlowSHDTO;
import cn.fl.project.service.*;
import cn.fl.project.transform.ProjectFlowSHVOTransform;
import cn.fl.project.vo.*;
import cn.fl.system.constant.SysLogConstant;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import fl.constant.facade.framework.constant.FlowConstant;
import fl.constant.facade.framework.constant.SysCdMapConstant;
import fl.flow.facade.flow.api.IFlowApiService;
import fl.flow.facade.flow.condition.SysFlowManageVQueryCondition;
import fl.flow.facade.flow.dto.FlowCompleteParamsDTO;
import fl.flow.facade.flow.dto.FlowStartParamsDTO;
import fl.flow.facade.flow.dto.FlowStartedDTO;
import fl.flow.facade.flow.dto.SysFlowManageVDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PrjAuditFlowServiceImpl implements IPrjAuditFlowService {

	private static final Logger logger = LoggerFactory.getLogger(PrjAuditFlowServiceImpl.class);
	
	/**
	 * 项目基础信息服务
	 */
	@Autowired
	private IPrjBscInfoService prjBscInfoService;
	
	/**
	 * 客户基础信息服务
	 */
	@Autowired
	private ICstBscInfoService cstBscInfoService;
	
	/**
	 * 项目客户基础信息服务
	 */
	@Autowired
	private IPrjCstBscInfoService prjCstBscInfoService;
	
	/**
	 * 项目报价方案信息服务
	 */
	@Autowired
	private IPrjQuatPrpsService prjQuatPrpsService;
	
	/**
	 * 项目费用信息服务
	 */
	@Autowired
	private IPrjQuatPrpsFeeService prjQuatPrpsFeeService;
	
	@Autowired
	private IProjectApiService projectApiService;
	
	@Autowired
	private IFlowApiService flowApiService;
	
	@Autowired
	private ISysCdMapApiService sysCdMapApiService;
	
	@Autowired
	private IAudStandingBookService audStandingBookService;
	
	@Autowired
	private IAuditApiService auditApiService;
	
	@Autowired
	private IProjectQueryApiService projectQueryApiService;
	
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Autowired
	private IAdvBizRecService advBizRecService;

	@Autowired
	private IPrjCstAccInfoService prjCstAccInfoService;

	@Autowired
	private ICapPayInfoService capPayInfoService;
	
	@Override
	public List<ProjectFlowSHVO> searchProjectFlowList(Long prjId) throws BaseException {
		if(StringUtils.isEmpty(prjId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		BaseLogUtils.info(logger, "searchProjectFlowList", "===查询项目流程跟踪数据Start===prjId:"+prjId);
		List<ProjectFlowSHDTO> DTOList = this.projectQueryApiService.searchProjectFlowList(prjId, CurrentThreadContext.getCurrentSysCd());
		BaseLogUtils.info(logger, "searchProjectFlowList", "===查询项目流程跟踪数据End===prjId:"+prjId+",DTOList:"+JSON.toJSONString(DTOList));
		return ProjectFlowSHVOTransform.toVOList(DTOList);
	}
	
	@Override
	public PrjAuditFlowVO searchPrjAuditFlowByPrjId(Long projectId) throws BaseException {
		if(null==projectId){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		PrjAuditFlowVO prjAuditFlowVO = new PrjAuditFlowVO();
		/**项目信息*/
		PrjBscInfoVO prjBscInfoVO = null;
		try {
			prjBscInfoVO = this.prjBscInfoService.selectById(projectId);
			prjAuditFlowVO.setPrjBscinfoVO(prjBscInfoVO);
		} catch (Exception e) {
			BaseLogUtils.info(logger, "searchPrjAuditFlowByPrjId", "查询项目基本信息失败.DETAIL:"+e.getMessage());
		}
		/**承租人信息*/
		if(null!=prjBscInfoVO && null!=prjBscInfoVO.getCstId()){
			try {
				CstBscInfoVO cstBscInfoVO = this.cstBscInfoService.selectDetailById(prjBscInfoVO.getCstId());
				prjAuditFlowVO.setCstBscInfoVO(cstBscInfoVO);
			} catch (Exception e) {
				BaseLogUtils.info(logger, "searchPrjAuditFlowByPrjId", "查询承租人信息失败.DETAIL:"+e.getMessage());
			}
		}
		/**项目报价方案信息*/
		List<PrjQuatPrpsVO> quatList= null;
		try {
			PrjQuatPrpsQueryCondition quatQC = new PrjQuatPrpsQueryCondition();
			quatQC.setIsFormalPrps(BaseConstant.IS_YESNO_YES);
			quatQC.setPrjId(projectId);
			quatList = this.prjQuatPrpsService.searchList(quatQC);
		} catch (Exception e) {
			BaseLogUtils.info(logger, "searchPrjAuditFlowByPrjId", "查询项目报价方案信息失败.DETAIL:"+e.getMessage());
		}
		if(CollectionUtils.isNotEmpty(quatList)){
			PrjQuatPrpsVO prjQuatPrpsVO = quatList.get(0);
			//处理报价方案中金额显示
			/**注释掉这两向金额处理原因:幺姐在fl-service的接口实现里加了这两项金额的处理,这里在处理就处理重复了*/
			//prjQuatPrpsVO.setTotLsItmAmt(DecimalUtils.div(prjQuatPrpsVO.getTotLsItmAmt(), 100));//租赁物总价值
			//prjQuatPrpsVO.setContAmt(DecimalUtils.div(prjQuatPrpsVO.getContAmt(), 100));//合同总额
			prjQuatPrpsVO.setActLsAmt(DecimalUtils.div(prjQuatPrpsVO.getActLsAmt(), 100));//实际融资租赁价值
			prjQuatPrpsVO.setEachEstRntAmt(DecimalUtils.div(prjQuatPrpsVO.getEachEstRntAmt(), 100));//每期预估租金	
			//处理年利率比例小数位
			if(null!=prjQuatPrpsVO.getAnnIntRatePct()){
				prjQuatPrpsVO.setAnnIntRatePct(DecimalUtils.round(prjQuatPrpsVO.getAnnIntRatePct(), 2));
			}
			
			prjAuditFlowVO.setPrjQuatPrpsVO(prjQuatPrpsVO);
			if(null!=prjQuatPrpsVO && null!=prjQuatPrpsVO.getId()){
				/**根据项目报价方案ID查询报价方案基础费用信息*/
				PrjQuatPrpsFeeQueryCondition feeQc = new PrjQuatPrpsFeeQueryCondition();
				feeQc.setPrpsId(prjQuatPrpsVO.getId());
				List<PrjQuatPrpsFeeVO> feeList = this.prjQuatPrpsFeeService.searchList(feeQc);//方案费用信息集合
				if(CollectionUtils.isNotEmpty(feeList)){
					for (PrjQuatPrpsFeeVO prjQuatPrpsFeeVO : feeList) {
						if(prjQuatPrpsFeeVO.getFeeTypCd().equals(PrjQuatPrpsFeeConstant.PRJ_FEE_TYP_FIRST_PRC)){//首期
							prjAuditFlowVO.setFirstFeeAmt(DecimalUtils.div(prjQuatPrpsFeeVO.getFeeAmt(), 100));//除100
							//比例数据四舍五入
							if(null!=prjQuatPrpsFeeVO.getFeePct()){
								prjAuditFlowVO.setFirstFeePct(DecimalUtils.round(prjQuatPrpsFeeVO.getFeePct(), 2));
							}
						}else if (prjQuatPrpsFeeVO.getFeeTypCd().equals(PrjQuatPrpsFeeConstant.PRJ_FEE_TYP_COLLATERAL)){//保证金
							prjAuditFlowVO.setAssureFeeAmt(DecimalUtils.div(prjQuatPrpsFeeVO.getFeeAmt(), 100));//除100
							//比例数据四舍五入
							if(null!=prjQuatPrpsFeeVO.getFeePct()){
								prjAuditFlowVO.setAssureFeePct(DecimalUtils.round(prjQuatPrpsFeeVO.getFeePct(), 2));
							}
						}else if (prjQuatPrpsFeeVO.getFeeTypCd().equals(PrjQuatPrpsFeeConstant.FEE_TYP_CD_08)){//供应商返利
							prjAuditFlowVO.setRebateAmt(DecimalUtils.div(prjQuatPrpsFeeVO.getFeeAmt(), 100));//除100
							//比例数据四舍五入
							if(null!=prjQuatPrpsFeeVO.getFeePct()){
								prjAuditFlowVO.setRebatePct(DecimalUtils.round(prjQuatPrpsFeeVO.getFeePct(), 2));
							}
						}else if(prjQuatPrpsFeeVO.getFeeTypCd().equals(PrjQuatPrpsFeeConstant.PRJ_FEE_TYP_CONSULT_1)){//咨询费1
							prjAuditFlowVO.setConsultFeeAmt(DecimalUtils.div(prjQuatPrpsFeeVO.getFeeAmt(), 100));//chu100
							//比例数据四舍五入
							if(null!=prjQuatPrpsFeeVO.getFeePct()){
								prjAuditFlowVO.setConsultFeePct(DecimalUtils.round(prjQuatPrpsFeeVO.getFeePct(), 2));
							}
						}else if(prjQuatPrpsFeeVO.getFeeTypCd().equals(PrjQuatPrpsFeeConstant.PRJ_FEE_TYP_CONSULT_2)){//咨询费2
							prjAuditFlowVO.setConsultFeeAmt1(DecimalUtils.div(prjQuatPrpsFeeVO.getFeeAmt(), 100));//chu100
							//比例数据四舍五入
							if(null!=prjQuatPrpsFeeVO.getFeePct()){
								prjAuditFlowVO.setConsultFeePct1(DecimalUtils.round(prjQuatPrpsFeeVO.getFeePct(), 2));
							}
							//咨询费2计算基数
							prjAuditFlowVO.setConsultFee2CalBaseCdNm(prjQuatPrpsFeeVO.getCalBaseCdNm());
						}else if(prjQuatPrpsFeeVO.getFeeTypCd().equals(PrjQuatPrpsFeeConstant.PRJ_FEE_TYP_PURCHASE)){//留购价
							prjAuditFlowVO.setPurchaseFeeAmt(DecimalUtils.div(prjQuatPrpsFeeVO.getFeeAmt(), 100));//chu100
						}else if (prjQuatPrpsFeeVO.getFeeTypCd().equals(PrjQuatPrpsFeeConstant.PRJ_FEE_TYP_INS_COLLATERAL)){//保险保证金
							prjAuditFlowVO.setInsAssureFeeAmt(DecimalUtils.div(prjQuatPrpsFeeVO.getFeeAmt(), 100));//除100
						}else if (prjQuatPrpsFeeVO.getFeeTypCd().equals(PrjQuatPrpsFeeConstant.PRJ_FEE_TYP_GPS)){//GPS费
							prjAuditFlowVO.setGps(DecimalUtils.div(prjQuatPrpsFeeVO.getFeeAmt(), 100));//除100
						}else if (prjQuatPrpsFeeVO.getFeeTypCd().equals(PrjQuatPrpsFeeConstant.PRJ_FEE_TYP_RESIDUAL)){//残值
							//比例数据四舍五入
							if(null!=prjQuatPrpsFeeVO.getFeePct()){
								prjAuditFlowVO.setResidualFeePct(DecimalUtils.round(prjQuatPrpsFeeVO.getFeePct(), 2));
							}
						}else if (prjQuatPrpsFeeVO.getFeeTypCd().equals(PrjQuatPrpsFeeConstant.PRJ_FEE_TYP_TOTAL_INSUR)){//保险总额
							prjAuditFlowVO.setTotalInsurAmt(DecimalUtils.div(prjQuatPrpsFeeVO.getFeeAmt(), 100));//除100
						}else if (prjQuatPrpsFeeVO.getFeeTypCd().equals(PrjQuatPrpsFeeConstant.PRJ_FEE_TYP_PURCHASE_TAX)){//购置税
							prjAuditFlowVO.setPurchaseTaxAmt(DecimalUtils.div(prjQuatPrpsFeeVO.getFeeAmt(), 100));//除100
						}else if (prjQuatPrpsFeeVO.getFeeTypCd().equals(PrjQuatPrpsFeeConstant.PRJ_FEE_TYP_UPKEEP)){//保养
							prjAuditFlowVO.setUpkeepAmt(DecimalUtils.div(prjQuatPrpsFeeVO.getFeeAmt(), 100));//除100
						}else if (prjQuatPrpsFeeVO.getFeeTypCd().equals(PrjQuatPrpsFeeConstant.PRJ_FEE_TYP_HARNESS)){//装具
							prjAuditFlowVO.setHarnessAmt(DecimalUtils.div(prjQuatPrpsFeeVO.getFeeAmt(), 100));//除100
						}else if (prjQuatPrpsFeeVO.getFeeTypCd().equals(PrjQuatPrpsFeeConstant.FEE_TYP_CD_56)){//额外上牌抵押费
							prjAuditFlowVO.setExtraMortgageAmt(DecimalUtils.div(prjQuatPrpsFeeVO.getFeeAmt(), 100));//除100
						}
					}
				}
			}
		}
		//附加费用合计
		Double additionAmt = 0D;
		if(prjAuditFlowVO.getTotalInsurAmt()!=null){
			additionAmt = DecimalUtils.add(additionAmt, prjAuditFlowVO.getTotalInsurAmt());
		}
		if(prjAuditFlowVO.getPurchaseTaxAmt()!=null){
			additionAmt = DecimalUtils.add(additionAmt, prjAuditFlowVO.getPurchaseTaxAmt());
		}
		if(prjAuditFlowVO.getUpkeepAmt()!=null){
			additionAmt = DecimalUtils.add(additionAmt, prjAuditFlowVO.getUpkeepAmt());
		}
		if(prjAuditFlowVO.getHarnessAmt()!=null){
			additionAmt = DecimalUtils.add(additionAmt, prjAuditFlowVO.getHarnessAmt());
		}
		prjAuditFlowVO.setAdditionAmt(additionAmt);
		/**项目客户账户信息*/
		PrjCstAccInfoVO prjCstAccInfoVO = null;
		try {
			PrjCstAccInfoQueryCondition prjCstAccInfoQC = new PrjCstAccInfoQueryCondition();
			prjCstAccInfoQC.setIsDel(BaseConstant.IS_YESNO_NO);
			prjCstAccInfoQC.setPrjId(projectId);
			if (null != prjBscInfoVO) {
				if (StringUtils.isNotEmpty(prjBscInfoVO.getCstId())) {
					prjCstAccInfoQC.setCstId(prjBscInfoVO.getCstId());
				}
				if (StringUtils.isNotEmpty(prjBscInfoVO.getInsCode())) {
					prjCstAccInfoQC.setInsCode(prjBscInfoVO.getInsCode());
				}
			}
			List<PrjCstAccInfoVO> prjCstAccInfoVOS = this.prjCstAccInfoService.searchList(prjCstAccInfoQC);
			if (CollectionUtils.isNotEmpty(prjCstAccInfoVOS)) {
				prjCstAccInfoVO = prjCstAccInfoVOS.get(0);
			}
			prjAuditFlowVO.setPrjCstAccInfoVO(prjCstAccInfoVO);
		} catch (Exception e) {
			BaseLogUtils.newWebLogger("查询项目客户账户信息失败.DETAIL:"+e.getMessage()).info();
		}
		return prjAuditFlowVO;
	}

	@Override
	public List<PrjCstBscInfoVO> searchGuarantorList(Long projectId) throws BaseException {
		if(StringUtils.isEmpty(projectId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		List<PrjCstBscInfoVO> guarantorList = new ArrayList<PrjCstBscInfoVO>();
		PrjCstBscInfoQueryCondition guarantorQC = new PrjCstBscInfoQueryCondition();
		guarantorQC.setPrjId(Long.valueOf(projectId));
		guarantorQC.setCstRlTyp(PrjCstBscInfoConstant.CST_Rl_ATTR_GUARANTOR);
		guarantorList = this.prjCstBscInfoService.searchList(guarantorQC);
		return guarantorList;
	}
	
	@Override
	public DataResultVO addFlowStart(DataResultVO result, FlowStartParamsDTO flowStartParamsDTO) throws BaseException {
		String prjId = flowStartParamsDTO.getBusinessKey();
		String procDefKey = flowStartParamsDTO.getProcDefKey();
		String schr1 = flowStartParamsDTO.getSchr1();
		BaseLogUtils.newWebLogger("===addFlowStart===prjId:"+prjId + ",procDefKey:" + procDefKey + ",schr1:" + schr1).info();
		//1.查询项目信息
		PrjBscInfoDTO prjBscInfoDTO = this.projectApiService.selectById(Long.valueOf(prjId));
		String prjTypCd = prjBscInfoDTO.getPrjTypCd();//项目类型
		String bizTypCd = prjBscInfoDTO.getBizTypCd();//业务模式
		String cstMgrOrgCd = prjBscInfoDTO.getCstMgrOrgCd();//客户经理组织架构
		if (StringUtils.isNotEmpty(schr1) && "1".equals(schr1)) {
			BaseLogUtils.newWebLogger("===当前用户组织架构===cd:"+JSON.toJSONString(CurrentThreadContext.getCurrentOrgCd()) + ",组织架构:" + CurrentThreadContext.getCurrentOrgShtNm()).info();
			SysCdMapDQueryCondition sysCdMapDQC = new SysCdMapDQueryCondition();
			sysCdMapDQC.setSysCd(CurrentThreadContext.getCurrentSysCd());
			sysCdMapDQC.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
			sysCdMapDQC.setmTypCd("M1076");
			List<String> getsCodeList = new ArrayList<>();
			getsCodeList.add("orgCd");
			sysCdMapDQC.setsCodeList(getsCodeList);
			BaseLogUtils.newWebLogger("===调ISysCdMapApiService查询映射的流程KEY开始===condition:"+JSON.toJSONString(sysCdMapDQC)).info();
			List<SysCdMapDDTO> sysCdMapDDTOs = this.sysCdMapApiService.searchSysCdMapDWithTargetList(sysCdMapDQC);
			BaseLogUtils.newWebLogger("===调ISysCdMapApiService查询映射的流程KEY结束===sysCdMapDDTOList:"+JSON.toJSONString(sysCdMapDDTOs)).info();
			boolean expFlowStartAbleFlag = false;
			if (CollectionUtils.isNotEmpty(sysCdMapDDTOs)) {
				for (SysCdMapDDTO sysCdMapDDTO : sysCdMapDDTOs) {
					String tCode = sysCdMapDDTO.getTCode();
					if (CurrentThreadContext.getCurrentOrgCd().contains(tCode) && StringUtils.isNotEmpty(cstMgrOrgCd) && cstMgrOrgCd.contains(tCode)) {
						expFlowStartAbleFlag = true;
					}
				}
			}
			BaseLogUtils.newWebLogger("===是否为可发起外派组织架构===expFlowStartAbleFlag:"+expFlowStartAbleFlag).info();
			if (!expFlowStartAbleFlag) {
				throw new BaseException("无发起外派流程权限.");
			}
			// todo 外派流程
			List<SysCdMapDDTO> sysCdMapDDTOList = this.getProcDefKey(cstMgrOrgCd,
					PropertiesCacheUtils.getProperty(SysCdMapConstant.M_TYP_CD_EXP_TYP_CD), PropertiesCacheUtils.getProperty(FlowConstant.FLOW_PROC_DEF_SIGN_EXP) + "|" + schr1);
			if(CollectionUtils.isNotEmpty(sysCdMapDDTOList) && sysCdMapDDTOList.size()==1){
				SysCdMapDDTO sysCdMapDDTO = sysCdMapDDTOList.get(0);
				String proKey = sysCdMapDDTO.getTCode();
				result = this.addFlowStartByProcDefKey(proKey, cstMgrOrgCd, result, flowStartParamsDTO);
				return result;
			} else {
				throw new BaseException("未找到能发起外派的流程.");
			}
		}

		if(StringUtils.isNotEmpty(procDefKey)){
			//流程KEY 不为空 根据流程KEY 查询最新可用版本并发起流程
			result = this.addFlowStartByProcDefKey(procDefKey, cstMgrOrgCd, result, flowStartParamsDTO);
			return result;
		}else{
			//2018-2-23 by yaoxiurong 三方资管项目 根据机构发起流程
			//REQ-3756 36资管机构路由 by yaoxiurong 作废第三方资管城配,第三方资管传统 新增字段IS_INS_BIZ标识是否资管业务
			if(null!=prjBscInfoDTO.getIsInsBiz() && BaseConstant.IS_YESNO_YES==prjBscInfoDTO.getIsInsBiz().intValue()){
				String insCode = prjBscInfoDTO.getInsCode();//机构编码
				if(StringUtils.isNotEmpty(insCode)){
					//查询机构编码 映射的 流程KEY
					List<SysCdMapDDTO> sysCdMapDDTOList1 = this.getProcDefKey(cstMgrOrgCd, 
							PropertiesCacheUtils.getProperty(SysCdMapConstant.M_TYP_CD_INS_CD), prjTypCd+"|"+insCode);
					if(CollectionUtils.isNotEmpty(sysCdMapDDTOList1) && sysCdMapDDTOList1.size()==1){
						//机构编码映射到流程KEY 存在且唯一  
						//根据流程KEY 查询流程最新可用版本 并发起流程
						SysCdMapDDTO sysCdMapDDTO = sysCdMapDDTOList1.get(0);
						String proKey = sysCdMapDDTO.getTCode();
						result = this.addFlowStartByProcDefKey(proKey, cstMgrOrgCd, result, flowStartParamsDTO);
						return result;
					}
				}
			}
			// REQ-3427 新能源选择浦发业务默认发起新能源快捷报单流程
			String buOrgCd = prjBscInfoDTO.getBuOrgCd();  // 项目所属事业部
			if(PrjBscInfoConstant.CODE_BU_ORG_CD_XNY.equals(buOrgCd)){
				String insCode = prjBscInfoDTO.getInsCode();//机构编码
				if(StringUtils.isNotEmpty(insCode) && PrjBscInfoConstant.PRJ_INS_CODE_SPD.equals(insCode)){
					//查询机构编码 映射的 流程KEY
					List<SysCdMapDDTO> sysCdMapDDTOList1 = this.getProcDefKey(buOrgCd,
							PropertiesCacheUtils.getProperty(SysCdMapConstant.M_TYP_CD_INS_CD), insCode);
					if(CollectionUtils.isNotEmpty(sysCdMapDDTOList1) && sysCdMapDDTOList1.size()==1){
						//机构编码映射到流程KEY 存在且唯一
						//根据流程KEY 查询流程最新可用版本 并发起流程
						SysCdMapDDTO sysCdMapDDTO = sysCdMapDDTOList1.get(0);
						String proKey = sysCdMapDDTO.getTCode();
						result = this.addFlowStartByProcDefKey(proKey, cstMgrOrgCd, result, flowStartParamsDTO);
						return result;
					}
				}
			}

            //REQ-5966 轻卡团单立项流程开发,轻卡立项流程发起逻辑调整
            if(PrjBscInfoConstant.CODE_BU_ORG_CD_QK.equals(buOrgCd)){
                if (null != prjBscInfoDTO.getIsGrpBiz()) {
                    String isGrpBizS = prjBscInfoDTO.getIsGrpBiz().toString();
                    //轻卡是否团单业务与流程关系映射代码
                    List<SysCdMapDDTO> sysCdMapDDTOList1 = this.getProcDefKey(buOrgCd, PropertiesCacheUtils.getProperty(SysCdMapConstant.M_TYP_CD_QK_GROUP_CD), isGrpBizS);
                    if(CollectionUtils.isNotEmpty(sysCdMapDDTOList1)) {
                        return this.addFlowStartByProcDefKey(sysCdMapDDTOList1.get(0).getTCode(), cstMgrOrgCd, result, flowStartParamsDTO);
                    }
                }
            }

			//查询项目类型  映射的 流程KEY 
			List<SysCdMapDDTO> sysCdMapDDTOList = this.getProcDefKey(cstMgrOrgCd, PropertiesCacheUtils.getProperty(SysCdMapConstant.M_TYP_CD_PRJ_TYP_CD), prjTypCd);
			if(CollectionUtils.isNotEmpty(sysCdMapDDTOList) && sysCdMapDDTOList.size()==1){
				//项目类型映射到流程KEY 存在且唯一  
				//根据流程KEY 查询流程最新可用版本 并发起流程
				SysCdMapDDTO sysCdMapDDTO = sysCdMapDDTOList.get(0);
				String proKey = sysCdMapDDTO.getTCode();
				result = this.addFlowStartByProcDefKey(proKey, cstMgrOrgCd, result, flowStartParamsDTO);
				return result;
			}else if(CollectionUtils.isNotEmpty(sysCdMapDDTOList) && sysCdMapDDTOList.size()>1){
				//项目类型映射到流程KEY 存在且不唯一  
				result.setSuccess(2);
				com.alibaba.fastjson.JSONArray resultArray = new com.alibaba.fastjson.JSONArray();
				for (SysCdMapDDTO sysCdMapDDTO : sysCdMapDDTOList) {
					com.alibaba.fastjson.JSONObject obj = new com.alibaba.fastjson.JSONObject();
					obj.put("procDefKey", sysCdMapDDTO.getTCode());//流程KEY
					obj.put("procNm", sysCdMapDDTO.getTCodeNm());//流程名称
					resultArray.add(obj);
				}
				result.setData(resultArray);
				return result;
			}else{
				//项目类型映射到流程KEY 不存在  
				//查询业务模式  映射的 流程KEY 
				List<SysCdMapDDTO> mapDList = this.getProcDefKey(cstMgrOrgCd, PropertiesCacheUtils.getProperty(SysCdMapConstant.M_TYP_CD_BIZ_TYP_CD), bizTypCd);
				if(CollectionUtils.isNotEmpty(mapDList) && mapDList.size()==1){
					//业务模式映射到流程KEY 存在且唯一
					//根据流程KEY 查询流程最新可用版本 并发起流程
					SysCdMapDDTO sysCdMapDDTO = mapDList.get(0);
					String proKey = sysCdMapDDTO.getTCode();
					result = this.addFlowStartByProcDefKey(proKey, cstMgrOrgCd, result, flowStartParamsDTO);
					return result;
				}else if(CollectionUtils.isNotEmpty(mapDList) && mapDList.size()>1){
					//业务模式映射到流程KEY 存在且不唯一  
					result.setSuccess(2);
					com.alibaba.fastjson.JSONArray resultArray = new com.alibaba.fastjson.JSONArray();
					for (SysCdMapDDTO sysCdMapDDTO : mapDList) {
						com.alibaba.fastjson.JSONObject obj = new com.alibaba.fastjson.JSONObject();
						obj.put("procDefKey", sysCdMapDDTO.getTCode());//流程KEY
						obj.put("procNm", sysCdMapDDTO.getTCodeNm());//流程名称
						resultArray.add(obj);
					}
					result.setData(resultArray);
					return result;
				}else{
					//业务模式映射到流程KEY 不存在  
					//查询当前组织架构所有立项流程
					SysFlowManageVQueryCondition flowManaQC = new SysFlowManageVQueryCondition();
					flowManaQC.setSysCd(CurrentThreadContext.getCurrentSysCd());
					flowManaQC.setIsDel(BaseConstant.IS_YESNO_NO);
					flowManaQC.setIsValid(BaseConstant.IS_YESNO_YES);
					flowManaQC.setModelTyp(flowStartParamsDTO.getModelTyp());
					flowManaQC.setOrgCd(cstMgrOrgCd);
					BaseLogUtils.info(logger, "addFlowStart", "===根据项目类型|业务模式皆未查询到可发起流程,查询客户经理组织架构下所有立项流程Start===flowManaQC:"+JSON.toJSONString(flowManaQC));
					List<SysFlowManageVDTO> sysFlowManageVDTOList = this.flowApiService.searchFlowManageVListForStart(flowManaQC);
					BaseLogUtils.info(logger, "addFlowStart", "===根据项目类型|业务模式皆未查询到可发起流程,查询客户经理组织架构下所有立项流程End===sysFlowManageVDTOList:"+JSON.toJSONString(sysFlowManageVDTOList));
					if(CollectionUtils.isNotEmpty(sysFlowManageVDTOList) && sysFlowManageVDTOList.size()==1){
						SysFlowManageVDTO sysFlowManageVDTO = sysFlowManageVDTOList.get(0);
						String proKey = sysFlowManageVDTO.getProcDefKey();
						result = this.addFlowStartByProcDefKey(proKey, cstMgrOrgCd, result, flowStartParamsDTO);
						return result;
					}else if(CollectionUtils.isNotEmpty(sysFlowManageVDTOList) && sysFlowManageVDTOList.size()>1){
						result.setSuccess(2);
						com.alibaba.fastjson.JSONArray resultArray = new com.alibaba.fastjson.JSONArray();
						for (SysFlowManageVDTO sysFlowManageVDTO : sysFlowManageVDTOList) {
							com.alibaba.fastjson.JSONObject obj = new com.alibaba.fastjson.JSONObject();
							obj.put("procDefKey", sysFlowManageVDTO.getProcDefKey());//流程KEY
							obj.put("procNm", sysFlowManageVDTO.getProcDefNm());//流程名称
							resultArray.add(obj);
						}
						result.setData(resultArray);
						return result;
					}else{
						result.setSuccess(BaseConstant.IS_YESNO_NO);
						result.setInfo("未找到能发起的流程.");
					}
				}
			}
			
		}
		
		return result;
	}

	@Override
	public DataResultVO addPreAudFlowStart(DataResultVO result, FlowStartParamsDTO flowStartParamsDTO) throws BaseException {

		BaseLogUtils.newWebLogger("===addPreAudFlowStart===result:"+JSONObject.toJSONString(result) + ",flowStartParamsDTO:" + JSONObject.toJSONString(flowStartParamsDTO)).info();
		String prjId = flowStartParamsDTO.getBusinessKey();
		String procDefKey = flowStartParamsDTO.getProcDefKey();
		//1.查询项目信息
		PrjBscInfoDTO prjBscInfoDTO = this.projectApiService.selectById(Long.valueOf(prjId));
		String prjTypCd = prjBscInfoDTO.getPrjTypCd();//项目类型
		String bizTypCd = prjBscInfoDTO.getBizTypCd();//业务模式
		String cstMgrOrgCd = prjBscInfoDTO.getCstMgrOrgCd();//客户经理组织架构

		if(StringUtils.isNotEmpty(procDefKey)){
			//流程KEY 不为空 根据流程KEY 查询最新可用版本并发起流程
			result = this.addPreAudFlowStartByProcDefKey(procDefKey, cstMgrOrgCd, result, flowStartParamsDTO);
			return result;
		}else{

			String buOrgCd = prjBscInfoDTO.getBuOrgCd();
			//查询项目类型  映射的 流程KEY
			List<SysCdMapDDTO> sysCdMapDDTOList = this.getProcDefKey(cstMgrOrgCd, PropertiesCacheUtils.getProperty(SysCdMapConstant.M_TYP_CD_PRJ_TYP_CD), prjTypCd);
			if(CollectionUtils.isNotEmpty(sysCdMapDDTOList) && sysCdMapDDTOList.size()==1){
				//项目类型映射到流程KEY 存在且唯一
				//根据流程KEY 查询流程最新可用版本 并发起流程
				SysCdMapDDTO sysCdMapDDTO = sysCdMapDDTOList.get(0);
				String proKey = sysCdMapDDTO.getTCode();
				result = this.addPreAudFlowStartByProcDefKey(proKey, cstMgrOrgCd, result, flowStartParamsDTO);
				return result;
			}else if(CollectionUtils.isNotEmpty(sysCdMapDDTOList) && sysCdMapDDTOList.size()>1){
				//项目类型映射到流程KEY 存在且不唯一
				result.setSuccess(2);
				com.alibaba.fastjson.JSONArray resultArray = new com.alibaba.fastjson.JSONArray();
				for (SysCdMapDDTO sysCdMapDDTO : sysCdMapDDTOList) {
					com.alibaba.fastjson.JSONObject obj = new com.alibaba.fastjson.JSONObject();
					obj.put("procDefKey", sysCdMapDDTO.getTCode());//流程KEY
					obj.put("procNm", sysCdMapDDTO.getTCodeNm());//流程名称
					resultArray.add(obj);
				}
				result.setData(resultArray);
				return result;
			}else{
				//项目类型映射到流程KEY 不存在
				//查询业务模式  映射的 流程KEY
				List<SysCdMapDDTO> mapDList = this.getProcDefKey(cstMgrOrgCd, PropertiesCacheUtils.getProperty(SysCdMapConstant.M_TYP_CD_BIZ_TYP_CD), bizTypCd);
				if(CollectionUtils.isNotEmpty(mapDList) && mapDList.size()==1){
					//业务模式映射到流程KEY 存在且唯一
					//根据流程KEY 查询流程最新可用版本 并发起流程
					SysCdMapDDTO sysCdMapDDTO = mapDList.get(0);
					String proKey = sysCdMapDDTO.getTCode();
					result = this.addPreAudFlowStartByProcDefKey(proKey, cstMgrOrgCd, result, flowStartParamsDTO);
					return result;
				}else if(CollectionUtils.isNotEmpty(mapDList) && mapDList.size()>1){
					//业务模式映射到流程KEY 存在且不唯一
					result.setSuccess(2);
					com.alibaba.fastjson.JSONArray resultArray = new com.alibaba.fastjson.JSONArray();
					for (SysCdMapDDTO sysCdMapDDTO : mapDList) {
						com.alibaba.fastjson.JSONObject obj = new com.alibaba.fastjson.JSONObject();
						obj.put("procDefKey", sysCdMapDDTO.getTCode());//流程KEY
						obj.put("procNm", sysCdMapDDTO.getTCodeNm());//流程名称
						resultArray.add(obj);
					}
					result.setData(resultArray);
					return result;
				}else{
					//业务模式映射到流程KEY 不存在
					//查询当前组织架构所有立项流程
					SysFlowManageVQueryCondition flowManaQC = new SysFlowManageVQueryCondition();
					flowManaQC.setSysCd(CurrentThreadContext.getCurrentSysCd());
					flowManaQC.setIsDel(BaseConstant.IS_YESNO_NO);
					flowManaQC.setIsValid(BaseConstant.IS_YESNO_YES);
					flowManaQC.setModelTyp(flowStartParamsDTO.getModelTyp());
					flowManaQC.setOrgCd(cstMgrOrgCd);
					BaseLogUtils.info(logger, "addFlowStart", "===根据项目类型|业务模式皆未查询到可发起流程,查询客户经理组织架构下所有立项流程Start===flowManaQC:"+JSON.toJSONString(flowManaQC));
					List<SysFlowManageVDTO> sysFlowManageVDTOList = this.flowApiService.searchFlowManageVListForStart(flowManaQC);
					BaseLogUtils.info(logger, "addFlowStart", "===根据项目类型|业务模式皆未查询到可发起流程,查询客户经理组织架构下所有立项流程End===sysFlowManageVDTOList:"+JSON.toJSONString(sysFlowManageVDTOList));
					if(CollectionUtils.isNotEmpty(sysFlowManageVDTOList) && sysFlowManageVDTOList.size()==1){
						SysFlowManageVDTO sysFlowManageVDTO = sysFlowManageVDTOList.get(0);
						String proKey = sysFlowManageVDTO.getProcDefKey();
						result = this.addPreAudFlowStartByProcDefKey(proKey, cstMgrOrgCd, result, flowStartParamsDTO);
						return result;
					}else if(CollectionUtils.isNotEmpty(sysFlowManageVDTOList) && sysFlowManageVDTOList.size()>1){
						result.setSuccess(2);
						com.alibaba.fastjson.JSONArray resultArray = new com.alibaba.fastjson.JSONArray();
						for (SysFlowManageVDTO sysFlowManageVDTO : sysFlowManageVDTOList) {
							com.alibaba.fastjson.JSONObject obj = new com.alibaba.fastjson.JSONObject();
							obj.put("procDefKey", sysFlowManageVDTO.getProcDefKey());//流程KEY
							obj.put("procNm", sysFlowManageVDTO.getProcDefNm());//流程名称
							resultArray.add(obj);
						}
						result.setData(resultArray);
						return result;
					}else{
						result.setSuccess(BaseConstant.IS_YESNO_NO);
						result.setInfo("未找到能发起的流程.");
					}
				}
			}
		}
		return result;
	}

	/**
	 * 
	 * @param mTypCd
	 * 			项目类型:PropertiesCacheUtils.getProperty(SysCdMapConstant.M_TYP_CD_PRJ_TYP_CD)
	 * 			业务模式:PropertiesCacheUtils.getProperty(SysCdMapConstant.M_TYP_CD_BIZ_TYP_CD)
	 * @param sCode
	 * @return
	 * @throws BaseException
	 */
	private List<SysCdMapDDTO> getProcDefKey(String orgCd,String mTypCd, String sCode)throws BaseException{
		SysCdMapDQueryCondition qc = new SysCdMapDQueryCondition();
		qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
		qc.setOrgCd(orgCd);
		qc.setmTypCd(mTypCd);
		List<String> getsCodeList = new ArrayList<>();
		getsCodeList.add(sCode);
		qc.setsCodeList(getsCodeList);
		BaseLogUtils.info(logger, "addFlowStart", "===调ISysCdMapApiService查询映射的流程KEY开始===condition:"+JSON.toJSONString(qc));
		List<SysCdMapDDTO> mapDList = this.sysCdMapApiService.searchSysCdMapDWithTargetList(qc);
		BaseLogUtils.info(logger, "addFlowStart", "===调ISysCdMapApiService查询映射的流程KEY结束===mapDList:"+JSON.toJSONString(mapDList));
		return mapDList;
	}
	
	/**
	 * 根据流程KEY查询流程最新可用版本并发起流程 返回结果
	 * @param procDefKey	流程定义KEY
	 * @param orgCd		组织架构代码
	 * @param result
	 * @param flowStartParamsDTO
	 * @return
	 * @throws BaseException
	 */
	private DataResultVO addFlowStartByProcDefKey(String procDefKey,String orgCd, DataResultVO result, FlowStartParamsDTO flowStartParamsDTO)throws BaseException{
		SysFlowManageVQueryCondition flowManaQC = new SysFlowManageVQueryCondition();
		flowManaQC.setSysCd(CurrentThreadContext.getCurrentSysCd());
		flowManaQC.setIsDel(BaseConstant.IS_YESNO_NO);
		flowManaQC.setIsValid(BaseConstant.IS_YESNO_YES);
		flowManaQC.setModelTyp(flowStartParamsDTO.getModelTyp());
		flowManaQC.setOrgCd(orgCd);
		flowManaQC.setProcDefKey(procDefKey);
		List<SysFlowManageVDTO> flowManageVListForStart = this.flowApiService.searchFlowManageVListForStart(flowManaQC);
		//查询到唯一流程版本
		if(CollectionUtils.isNotEmpty(flowManageVListForStart) && flowManageVListForStart.size()==1){
			//根据此流程版本发起流程
			SysFlowManageVDTO sysFlowManageVDTO = flowManageVListForStart.get(0);
			flowStartParamsDTO.setProcDefId(sysFlowManageVDTO.getProcDefId());
			flowStartParamsDTO.setProcDefKey(sysFlowManageVDTO.getProcDefKey());
			flowStartParamsDTO.setProcNo(sysFlowManageVDTO.getProcNo());
			BaseLogUtils.info(logger, "addFlowStart", "===调IProjectApiService发起流程Start===flowStartParamsDTO:"+JSON.toJSONString(flowStartParamsDTO));
			DataResultDTO dataResultDTO = this.projectApiService.addFlowStart(flowStartParamsDTO);
			BaseLogUtils.info(logger, "addFlowStart", "===调IProjectApiService发起流程End===dataResultDTO:"+JSON.toJSONString(dataResultDTO));
			if(!dataResultDTO.getCode().equals(BaseExceptionConstant.BASE_EXCEPTION_CODE_200)){//流程发起失败
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_OPERATE_FAIL);
			}else if(null!=dataResultDTO && null!=dataResultDTO.getData()){
//				FlowStartedDTO flowStartedDTO = (FlowStartedDTO) dataResultDTO.getData();
                String data = JSON.toJSONString(dataResultDTO.getData());
                JSONObject jsonObject = JSON.parseObject(data);
                FlowStartedDTO flowStartedDTO = JSONObject.toJavaObject(jsonObject, FlowStartedDTO.class);
                if(StringUtils.isNotBlank(flowStartedDTO.getNextOperUsrNames())){
	                result.setInfo("下一个节点操作人:"+flowStartedDTO.getNextOperUsrNames());
	            }else{
	                result.setInfo("流程发起成功");
	            }
				String pNo = flowStartedDTO.getProcInstanceNo();//流程编号
				com.alibaba.fastjson.JSONArray resultArray = new com.alibaba.fastjson.JSONArray();
				com.alibaba.fastjson.JSONObject obj = new com.alibaba.fastjson.JSONObject();
				obj.put("pNo", pNo);
				resultArray.add(obj);
				result.setData(resultArray);
				
				//生成《所有权转让协议-首页》-发送MQ
				//重卡城配项目评审流程（FLOW_00000001_001_001）、三方资管城配业务工行评审（FLOW_00000001_001_004）、新能源汽车快捷报单流程（FLOW_00000013_001_002）
				if("FLOW_00000001_001_001".equals(procDefKey) || "FLOW_00000001_001_004".equals(procDefKey) || "FLOW_00000013_001_002".equals(procDefKey)) {
					String prjId = flowStartParamsDTO.getBusinessKey(); //项目ID
					if(StringUtils.isNotEmpty(prjId)) {
						FlowCompleteParamsDTO flowCompleteParamsDTO = new FlowCompleteParamsDTO();
						flowCompleteParamsDTO.setCrtUsrId(flowStartParamsDTO.getCrtUsrId());
						flowCompleteParamsDTO.setProcDefKey(flowStartParamsDTO.getProcDefKey());
						flowCompleteParamsDTO.setBusinessKey(Long.valueOf(prjId));
						flowCompleteParamsDTO.setSysCd(flowStartParamsDTO.getSysCd());
						flowCompleteParamsDTO.setOrgCd(flowStartParamsDTO.getOrgCd());
						this.projectApiService.sendCreateAgreementMsg(Long.valueOf(prjId), flowCompleteParamsDTO);
						BaseLogUtils.info(logger, "addFlowStart", "===生成《所有权转让协议-首页》-发送MQ===flowCompleteParamsDTO:"+JSON.toJSONString(flowCompleteParamsDTO));
					}
				}

				// 重卡城配流程同时发起垫款 增加垫款记录
				String schr2 = flowStartParamsDTO.getSchr2();
				if (StringUtils.isNotEmpty(schr2) && "1".equals(schr2)
						&& PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.FlowConstant.FLOW_PROC_DEF_KEY_TRUCK_CITY).equals(flowStartParamsDTO.getProcDefKey())) {
					String prjId = flowStartParamsDTO.getBusinessKey();
					AdvBizRecQC advBizRecQC = new AdvBizRecQC();
					advBizRecQC.setDataId(Long.valueOf(prjId));
					advBizRecQC.setIsDel(BaseConstant.IS_YESNO_NO);
					advBizRecQC.setDataTypCd("PRJ");
					BaseLogUtils.newWebLogger("查询随州垫款流程记录发起Start,prjId:" + prjId + ",advBizRecQC:" + JSON.toJSONString(advBizRecQC)).info();
					List<AdvBizRecVO> advBizRecVOS = this.advBizRecService.searchList(advBizRecQC);
					BaseLogUtils.newWebLogger("查询随州垫款流程记录发起End,prjId:" + prjId + ",advBizRecVOS:" + JSON.toJSONString(advBizRecVOS)).info();
					AdvBizRecVO advBizRec = new AdvBizRecVO();
					advBizRec.setDataId(Long.valueOf(prjId));
					advBizRec.setDataTypCd("PRJ");
					advBizRec.setAdvStsCd("0");
					advBizRec.setIsDel(BaseConstant.IS_YESNO_NO);
					if (CollectionUtils.isNotEmpty(advBizRecVOS)) {
						for (AdvBizRecVO advBizRecVO : advBizRecVOS) {
							String advTypCd = advBizRecVO.getAdvStsCd();
							if ("0".equals(advTypCd)) {
								advBizRecVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
								advBizRecVO.setMdfTm(new Date());
								boolean modify = this.advBizRecService.modify(advBizRecVO);
								BaseLogUtils.newWebLogger("已存在待发起的随州垫款流程,prjId:" + prjId + ",modify:" + modify).info();
							}
							if ("1".equals(advTypCd)) {
								throw new BaseException("随州垫款流程已发起，不可再次发起");
							}
							if ("2".equals(advTypCd)) {
								throw new BaseException("随州垫款流程已完成，不可再次发起");
							}
						}
					} else {
						advBizRec.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
						advBizRec.setCrtTm(new Date());
						BaseLogUtils.newWebLogger("随州垫款记录新增Start,prjId:" + prjId + ",advBizRec:" + JSON.toJSONString(advBizRec)).info();
						Long id = this.advBizRecService.add(advBizRec);
						if (StringUtils.isEmpty(id)) {
							throw new BaseException("随州垫款记录新增失败！");
						}
						BaseLogUtils.newWebLogger("随州垫款记录新增End,prjId:" + prjId + ",id:" + id).info();
					}
				}
				
				/**
				 * 流程发起成功 生成核价台账(卡车业务:包括卡车和卡车类客车)
				 */
				//改为service服务端生成
				/*try {
					PrjPrdBscInfoQueryCondition ppbiQC = new PrjPrdBscInfoQueryCondition();
					ppbiQC.setPrjId(Long.valueOf(flowStartParamsDTO.getBusinessKey()));
					BaseLogUtils.info(logger, "addFlowStartByProcDefKey", "===查询项目产品信息Start===ppbiQC:"+JSON.toJSONString(ppbiQC));
					List<PrjPrdBscInfoDTO> prjPrdList = this.projectApiService.searchPrjPrdList(ppbiQC, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
					BaseLogUtils.info(logger, "addFlowStartByProcDefKey", "===查询项目产品信息Start===prjPrdList:"+JSON.toJSONString(prjPrdList));
					
					List<AudStandingBookVO> voList = new ArrayList<AudStandingBookVO>();
					AudStandingBookVO audStandingBookVO = null;
					String procInstanceId = flowStartedDTO.getProcInstanceId();//流程实例ID
					Long prjId = Long.valueOf(flowStartParamsDTO.getBusinessKey());//项目ID
					if(CollectionUtils.isNotEmpty(prjPrdList)){
						for (PrjPrdBscInfoDTO prjPrdBscInfoDTO : prjPrdList) {
							//判断产品是否为卡车 是的话需要生成核价台账
							String prdTypCd = prjPrdBscInfoDTO.getPrdTypCd();
							if(StringUtils.isNotEmpty(prdTypCd)){
								prdTypCd = prdTypCd.substring(0, ProductConstant.PRD_TYE_LENGHT);
								if(prdTypCd.equals(ProductConstant.PRD_TK_CD_1)
										|| prdTypCd.equals(ProductConstant.PRD_TK_CD_2)
										|| prdTypCd.equals(ProductConstant.PRD_TK_CD_3)
										|| prdTypCd.equals(ProductConstant.PRD_TK_CD_4)
										|| prdTypCd.equals(ProductConstant.PRD_TK_CD_5)
										|| prdTypCd.equals(ProductConstant.PRD_TK_CD_6)
										|| prdTypCd.equals(ProductConstant.PRD_TK_CD_7)
										|| prdTypCd.equals(ProductConstant.PRD_TK_CD_8)
										|| prdTypCd.equals(ProductConstant.PRD_TK_CD_9)
										|| prdTypCd.equals(ProductConstant.PRD_TK_CD_10)
										|| prdTypCd.equals(ProductConstant.PRD_TK_CD_11)){
									audStandingBookVO = new AudStandingBookVO();
									audStandingBookVO.setProjectId(prjId);//项目ID
									audStandingBookVO.setPrjPrdId(prjPrdBscInfoDTO.getId());//项目产品ID
									audStandingBookVO.setProIntId(procInstanceId);//流程实例ID
									audStandingBookVO.setBookTypeCd(AudStandingBookConstant.AUD_TYP_CD_PRCE);//台账类型-核价台账
									audStandingBookVO.setReviewResultCd(AudStandingBookConstant.AUD_REVIEW_RESULT_CD_BEING);//审核中
									//audStandingBookVO.setLenWidHig(lenWidHig);
									String previewCorePrcInfo = this.audStandingBookService.getPreviewCorePrcInfo(prjPrdBscInfoDTO.getId());
									audStandingBookVO.setPricingInfo(previewCorePrcInfo);//核价信息
									audStandingBookVO.setCrtTm(new Date());
									audStandingBookVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
									voList.add(audStandingBookVO);
								}
							}
						}
					}
					if(CollectionUtils.isNotEmpty(voList)){
						//批量生成核价台账
						this.auditApiService.batchAddAudStandingBook(AudStandingBookVOTransform.toDTOList(voList), CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
					}
				} catch (Exception e) {
					BaseLogUtils.info(logger, "addFlowStartByProcDefKey", "===生成核价台账失败===Detail:"+e.getMessage());
			BaseLogUtils.error(logger, "-", "异常", e);
					String info = result.getInfo();
					result.setInfo(info+"[生成核价台账失败]");
				}*/
				
			}
		}else{
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("流程发起失败,未找到要发起的流程或流程版本不唯一.");
		}
		return result;
	}

	private DataResultVO addPreAudFlowStartByProcDefKey(String procDefKey,String orgCd, DataResultVO result, FlowStartParamsDTO flowStartParamsDTO)throws BaseException{
		SysFlowManageVQueryCondition flowManaQC = new SysFlowManageVQueryCondition();
		flowManaQC.setSysCd(CurrentThreadContext.getCurrentSysCd());
		flowManaQC.setIsDel(BaseConstant.IS_YESNO_NO);
		flowManaQC.setIsValid(BaseConstant.IS_YESNO_YES);
		flowManaQC.setModelTyp(flowStartParamsDTO.getModelTyp());
		flowManaQC.setOrgCd(orgCd);
		flowManaQC.setProcDefKey(procDefKey);
		List<SysFlowManageVDTO> flowManageVListForStart = this.flowApiService.searchFlowManageVListForStart(flowManaQC);
		//查询到唯一流程版本
		if(CollectionUtils.isNotEmpty(flowManageVListForStart) && flowManageVListForStart.size()==1){
			//根据此流程版本发起流程
			SysFlowManageVDTO sysFlowManageVDTO = flowManageVListForStart.get(0);
			flowStartParamsDTO.setProcDefId(sysFlowManageVDTO.getProcDefId());
			flowStartParamsDTO.setProcDefKey(sysFlowManageVDTO.getProcDefKey());
			flowStartParamsDTO.setProcNo(sysFlowManageVDTO.getProcNo());
			BaseLogUtils.info(logger, "addPreAudFlowStart", "===调IProjectApiService发起流程Start===flowStartParamsDTO:"+JSON.toJSONString(flowStartParamsDTO));
			DataResultDTO dataResultDTO = this.projectApiService.addPreAudFlowStart(flowStartParamsDTO);
			BaseLogUtils.info(logger, "addPreAudFlowStart", "===调IProjectApiService发起流程End===dataResultDTO:"+JSON.toJSONString(dataResultDTO));
			if(!dataResultDTO.getCode().equals(BaseExceptionConstant.BASE_EXCEPTION_CODE_200)){//流程发起失败
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_OPERATE_FAIL);
			}else if(null!=dataResultDTO && null!=dataResultDTO.getData()){
				String data = JSON.toJSONString(dataResultDTO.getData());
				JSONObject jsonObject = JSON.parseObject(data);
				FlowStartedDTO flowStartedDTO = JSONObject.toJavaObject(jsonObject, FlowStartedDTO.class);
				if(StringUtils.isNotBlank(flowStartedDTO.getNextOperUsrNames())){
					result.setInfo("下一个节点操作人:"+flowStartedDTO.getNextOperUsrNames());
				}else{
					result.setInfo("流程发起成功");
				}
				String pNo = flowStartedDTO.getProcInstanceNo();//流程编号
				com.alibaba.fastjson.JSONArray resultArray = new com.alibaba.fastjson.JSONArray();
				com.alibaba.fastjson.JSONObject obj = new com.alibaba.fastjson.JSONObject();
				obj.put("pNo", pNo);
				resultArray.add(obj);
				result.setData(resultArray);
			}
		}else{
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("流程发起失败,未找到要发起的流程或流程版本不唯一.");
		}
		return result;
	}

	@Override
	public List<KeyValueVO> searchSysDicCdList(String codeGrpCd, String sysCd,
			String orgCd) throws BaseException {
		if(StringUtils.isEmpty(codeGrpCd) || StringUtils.isEmpty(orgCd) || StringUtils.isEmpty(sysCd)){
			BaseLogUtils.info(logger, "searchSysDicCdList", "===查询数据字典数据请求参数不完整===codeGrpCd:"+codeGrpCd+",sysCd:"+sysCd+",orgCd:"+orgCd, CurrentThreadContext.getCurrentUserName());
			return new ArrayList<KeyValueVO>();
		}
		List<KeyValueVO> keyValueList = null;
		List<SysDictionaryDTO> dictionaryList = this.sysDictionaryApiService
				.searchGroup(sysCd, orgCd, codeGrpCd);
		if (CollectionUtils.isNotEmpty(dictionaryList)) {
			keyValueList = new ArrayList<>();
			for (SysDictionaryDTO sysDictionaryDTO : dictionaryList) {
				KeyValueVO keyValue = new KeyValueVO();
				keyValue.setKey(sysDictionaryDTO.getCode());
				keyValue.setValue(sysDictionaryDTO.getCodeNm());
				keyValueList.add(keyValue);
			}
		}
		return keyValueList;
	}

	@Override
	public DataResultVO checkExpatriateFlowAuth(String prjId, String capPayId) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "可发起随州垫款流程");
		DataResultDTO resultDTO = this.projectApiService.checkExpatriateFlowAuth(prjId, capPayId);
		if (null != resultDTO && BaseConstant.IS_YESNO_YES == resultDTO.getSuccess()) {
			// 城配提示，其他不提示
			if (StringUtils.isEmpty(capPayId)) {
				BaseLogUtils.newWebLogger("===查询项目信息Start===prjId:"+prjId).info();
				//1.查询项目信息
				PrjBscInfoDTO prjBscInfoDTO = this.projectApiService.selectById(Long.valueOf(prjId));
				BaseLogUtils.newWebLogger("===查询项目信息End===prjBscInfoDTO:"+JSON.toJSONString(prjBscInfoDTO)).info();
				List<String> keyList = new ArrayList<>();
				if (null != prjBscInfoDTO) {
					String prjTypCd = prjBscInfoDTO.getPrjTypCd();//项目类型
					String bizTypCd = prjBscInfoDTO.getBizTypCd();//业务模式
					String cstMgrOrgCd = prjBscInfoDTO.getCstMgrOrgCd();//客户经理组织架构

					List<SysCdMapDDTO> sysCdMapDDTOList = this.getProcDefKey(cstMgrOrgCd, PropertiesCacheUtils.getProperty(SysCdMapConstant.M_TYP_CD_PRJ_TYP_CD), prjTypCd);
					BaseLogUtils.newWebLogger("===查询流程版本1End===sysCdMapDDTOList:"+JSON.toJSONString(sysCdMapDDTOList)).info();
					if (CollectionUtils.isNotEmpty(sysCdMapDDTOList)) {
						keyList = this.getProDefKeyList(sysCdMapDDTOList, cstMgrOrgCd);
					} else {
						List<SysCdMapDDTO> mapDList = this.getProcDefKey(cstMgrOrgCd, PropertiesCacheUtils.getProperty(SysCdMapConstant.M_TYP_CD_BIZ_TYP_CD), bizTypCd);
						BaseLogUtils.newWebLogger("===查询流程版本2End===mapDList:"+JSON.toJSONString(mapDList)).info();
						keyList = this.getProDefKeyList(mapDList, cstMgrOrgCd);
					}
					BaseLogUtils.newWebLogger("===可发起流程key集合===keyList:"+JSON.toJSONString(keyList)).info();
				}
				if (CollectionUtils.isNotEmpty(keyList) && keyList.size() == 1
						&& PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.FlowConstant.FLOW_PROC_DEF_KEY_TRUCK_CITY).equals(keyList.get(0))) {
					// 只发起重卡城配
					result.setSuccess(BaseConstant.IS_YESNO_YES);
				} else {
					result.setSuccess(BaseConstant.IS_YESNO_NO);
				}
				result.setInfo(resultDTO.getInfo());
			} else {
				result.setSuccess(resultDTO.getSuccess());
				result.setInfo(resultDTO.getInfo());
			}
		} else {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("未获取到校验结果");
		}
		return result;
	}

	private List<String> getProDefKeyList(List<SysCdMapDDTO> sysCdMapDDTOList, String cstMgrOrgCd) {
		List<String> keyList = new ArrayList<>();
		for (SysCdMapDDTO sysCdMapDDTO : sysCdMapDDTOList) {
			String proKey = sysCdMapDDTO.getTCode();
			if (StringUtils.isNotEmpty(proKey)) {
				SysFlowManageVQueryCondition flowManaQC = new SysFlowManageVQueryCondition();
				flowManaQC.setSysCd(CurrentThreadContext.getCurrentSysCd());
				flowManaQC.setIsDel(BaseConstant.IS_YESNO_NO);
				flowManaQC.setIsValid(BaseConstant.IS_YESNO_YES);
				flowManaQC.setModelTyp(fl.flow.facade.flow.constant.FlowConstant.FLOW_MODEL_TYP_PRJ_1);
				flowManaQC.setOrgCd(cstMgrOrgCd);
				flowManaQC.setProcDefKey(proKey);
				List<SysFlowManageVDTO> flowManageVListForStart = this.flowApiService.searchFlowManageVListForStart(flowManaQC);
				BaseLogUtils.newWebLogger("===查询流程版本End===flowManageVListForStart:"+JSON.toJSONString(flowManageVListForStart)).info();
				for (SysFlowManageVDTO sysFlowManageVDTO : flowManageVListForStart) {
					String procDefKey = sysFlowManageVDTO.getProcDefKey();
					if (CollectionUtils.isEmpty(keyList) || !keyList.contains(procDefKey)) {
						keyList.add(procDefKey);
					}
				}
			}
		}
		return keyList;
	}

}
